<?php
    class home_index_controller extends controllerBase
    {
        public function process()
        {
            core::checkSession();
            $oDatabase = new database();

            // Get top 10 san pham duoc nhieu luot view nhat
            $sQuery = "
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
            PREFIX bus: <".URL::getLink('id/bussiness#').">

            SELECT *  
            WHERE {
            ?uri rdf:type bus:product.
            ?uri bus:name ?name.
            ?uri bus:price ?price.
            ?uri bus:defaultImage ?image.
            OPTIONAL{?uri bus:isHightLight ?isHightLight.}
            OPTIONAL{?uri bus:hightLightImage ?hightLightImage.}
            ?uri bus:location ?location.
            ?uri bus:description ?description.
            ?uri bus:publishTime ?time.
            ?uri bus:hasManufacturer ?manufacturerUri.
            ?manufacturerUri bus:name ?manufacturerName.
            ?uri bus:ownUser ?userUri.
            ?userUri foaf:familyName ?ownUser.
            OPTIONAL
            {
            ?uri bus:otherProperties ?otherUri.
            ?otherUri bus:totalView ?totalView.
            }
            }
            ORDER BY DESC(?totalView)
            LIMIT 10
            ";
            $result = $oDatabase->loadQuery($sQuery);
            $this->aParams['top10Product'] = $result['result']['rows'];

            // Get All Products
            $sQuery = "
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
            PREFIX bus: <".URL::getLink('id/bussiness#').">

            SELECT *  
            WHERE {
            ?uri rdf:type bus:product.
            ?uri bus:name ?name.
            ?uri bus:price ?price.
            ?uri bus:defaultImage ?image.
            OPTIONAL{?uri bus:isHightLight ?isHightLight.}
            OPTIONAL{?uri bus:hightLightImage ?hightLightImage.}
            ?uri bus:location ?location.
            ?uri bus:description ?description.
            ?uri bus:publishTime ?time.
            ?uri bus:hasManufacturer ?manufacturerUri.
            ?manufacturerUri bus:name ?manufacturerName.
            ?uri bus:ownUser ?userUri.
            ?userUri foaf:familyName ?ownUser.
            }
            ORDER BY DESC(?time)
            LIMIT 10 
            OFFSET 0
            ";
            $aData = $oDatabase->loadQuery($sQuery);
            if(count($aData['result']['rows']) > 0)
            {
                $this->aParams['aProducts'] = $aData['result']['rows'];
            }
            // Get hot product
            if(!caching::hasCache('aHotProducts'))
            {
                $aHotproducts = caching::createCacheHotProducts();       
            }
            else
            {
                $aHotproducts = caching::getCache('aHotProducts');
            }
            $this->aParams['aHotProducts'] = $aHotproducts;
            
            // User suggesstion
            if(database::isUser())
            {
                $sUserId = end(explode('/',$_SESSION['user_uri']));
                // Get category filter
                $sQuery = "SELECT * FROM user_suggestion WHERE type = 'category' AND user_id = '".$sUserId."'";
                $aListFilters = $oDatabase->getListObject($sQuery,false,true);
                $sConditionFilter = '';
                if(count($aListFilters) > 0)
                {
                    $sConditionFilter = 'FILTER(';
                    foreach($aListFilters as $aCondition)
                    {
                        $sConditionFilter = $sConditionFilter.$aCondition['filter'].' || ';
                    }
                    $sConditionFilter = trim($sConditionFilter," || ");
                    $sConditionFilter = $sConditionFilter.').';
                }
                // Get menufacturer filter
                $sQuery = "SELECT * FROM user_suggestion WHERE type = 'menufacturer' AND user_id = '".$sUserId."'";
                $aListFilters = $oDatabase->getListObject($sQuery,false,true);
                $sMenufacturerConditionFilter = '';
                if(count($aListFilters) > 0)
                {
                    $sMenufacturerConditionFilter = 'FILTER(';
                    foreach($aListFilters as $aCondition)
                    {
                        $sMenufacturerConditionFilter = $sMenufacturerConditionFilter.$aCondition['filter'].' || ';
                    }
                    $sMenufacturerConditionFilter = trim($sMenufacturerConditionFilter," || ");
                    $sMenufacturerConditionFilter = $sMenufacturerConditionFilter.').';
                }
                
                $sQuery = "
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
                PREFIX bus: <".URL::getLink('id/bussiness#').">

                SELECT *  
                WHERE {
                ?uri rdf:type bus:product.
                ?uri bus:name ?name.
                ?uri bus:price ?price.
                ?uri bus:defaultImage ?image.
                OPTIONAL{?uri bus:isHightLight ?isHightLight.}
                OPTIONAL{?uri bus:hightLightImage ?hightLightImage.}
                ?uri bus:hasCategory ?categoryUri.
                ?uri bus:location ?location.
                ?uri bus:description ?description.
                ?uri bus:publishTime ?time.
                ?uri bus:hasManufacturer ?menufacturerUri.
                ?menufacturerUri bus:name ?manufacturerName.
                ?uri bus:ownUser ?userUri.
                ?userUri foaf:familyName ?ownUser."
                .$sConditionFilter.
                $sMenufacturerConditionFilter.
                "}
                ORDER BY DESC(?time)
                LIMIT 10 
                OFFSET 0
                ";

                $aResult = $oDatabase->loadQuery($sQuery);
                if(!empty($aResult['result']['rows']) && count($aResult['result']['rows']) > 0)
                {
                    $this->aParams['aSuggetionProduct'] = $aResult['result']['rows'];
                }
            }
        }
    }
?>
